package com.skytech.tax.customsdatawarehouse.singlewindow.customsdeclarations.domain.dao;

import com.skytech.tax.customsdatawarehouse.singlewindow.customsdeclarations.domain.entity.IECustomsDeclarationRetFile;
import org.joda.time.DateTime;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @author Lianhong
 * @since 2021/11/30
 */
@Repository("singleWindow.ieCustomsDeclarationRetFileDao")
public class IECustomsDeclarationRetFileDao {

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private String existsCustomsDeclarationRetFileSql = "SELECT count(1) FROM gd_jckhwgd_ret_file WHERE cusRetSeqNo = ?";

    public boolean exists(String cusRetSeqNo) {
        Integer count = namedParameterJdbcTemplate.getJdbcOperations()
                .queryForObject(this.existsCustomsDeclarationRetFileSql, new Object[]{cusRetSeqNo}, Integer.class);
        return count != null && count > 0;
    }

    private String selectCustomsDeclarationRetFileSql = "SELECT file FROM gd_jckhwgd_ret_file WHERE cusRetSeqNo = ?";

    public String findByCusRetSeqNo(String cusRetSeqNo) {
        return namedParameterJdbcTemplate.getJdbcOperations().queryForObject(selectCustomsDeclarationRetFileSql,
                new Object[]{cusRetSeqNo}, String.class);
    }

    private String insertCustomsDeclarationRetFileSql = "INSERT INTO gd_jckhwgd_ret_file (cusRetSeqNo, file, storageTime) VALUES (:cusRetSeqNo, :file, :storageTime)";

    public void save(IECustomsDeclarationRetFile customsDeclarationRetFile) {
        customsDeclarationRetFile.setStorageTime(DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
        this.namedParameterJdbcTemplate.update(this.insertCustomsDeclarationRetFileSql, new BeanPropertySqlParameterSource(customsDeclarationRetFile));
    }
}
